package TestPaperSystem.service.impl;

import TestPaperSystem.Dto.ExamLimitDTO;
import TestPaperSystem.Dto.PaperVo;
import TestPaperSystem.entity.ExamLimit;
import TestPaperSystem.entity.User;
import TestPaperSystem.mapper.ExamLimitMapper;
import TestPaperSystem.service.ExamLimitService;
import TestPaperSystem.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExamLimitServiceImpl extends ServiceImpl<ExamLimitMapper, ExamLimit> implements ExamLimitService {

    @Resource
    ExamLimitMapper examLimitMapper;

    @Autowired
    UserService userService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveData(List<ExamLimit> examLimits) {

        LambdaQueryWrapper<ExamLimit> lambdaQueryWrapper = new LambdaQueryWrapper();

        List<ExamLimit> user_exist = examLimits.stream().filter(e ->
                userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUserName,e.getUserName())) != null)
                .collect(Collectors.toList());

        List<ExamLimit> list_db = examLimitMapper.selectList(lambdaQueryWrapper.eq(ExamLimit::getExamId , examLimits.get(0).getExamId())) ;

        if(list_db.isEmpty() && !user_exist.isEmpty())
            return this.saveBatch(user_exist);

        if(user_exist.isEmpty())
            return false;
        //        新增数据
        List<ExamLimit> not_exist_in_db = user_exist.stream().filter(e ->
                !list_db.stream().map(ExamLimit::getUserName).collect(Collectors.toList())
                        .contains(e.getUserName())
        ).collect(Collectors.toList());

        //        应删除数据
        List<String> should_del_in_db = list_db.stream().filter(e ->
                !user_exist.stream().map(ExamLimit::getUserName).collect(Collectors.toList())
                        .contains(e.getUserName())).map(ExamLimit::getId).collect(Collectors.toList());

        if(not_exist_in_db.isEmpty() && should_del_in_db.isEmpty()){
            return false;
        }else if(not_exist_in_db.isEmpty()){
            return (examLimitMapper.deleteBatchIds(should_del_in_db) > 0);
        }else if(should_del_in_db.isEmpty()){
            return this.saveBatch(not_exist_in_db);
        }else{
            return (examLimitMapper.deleteBatchIds(should_del_in_db) > 0) & this.saveBatch(not_exist_in_db);
        }


    }


    /*
    * 返回表示 表中含有此人信息返回true 否则false
    * */
    @Override
    public Boolean getQualify(PaperVo paper) {

        LambdaQueryWrapper<ExamLimit> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(paper.getId()!= null , ExamLimit::getUserName , paper.getId())
                .eq(paper.getExamId() != null , ExamLimit::getExamId , paper.getExamId());

        return examLimitMapper.selectCount(queryWrapper) > 0 ;

    }
}
